package aaaPKg;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Clob;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.attilax.db.DbServiceV5r4d;
import com.attilax.exception.ExUtil;
import com.attilax.io.pathx;
import com.attilax.ioc.Ioc4wash;
import com.attilax.json.AtiJson;
import com.attilax.persistence.DBCfg;
import com.attilax.persistence.DbutilX;
import com.attilax.time.timeUtil;
import com.attilax.user.UserSync4jonlye;
import com.google.common.base.Optional;

public class RefreshDeadLimit {

	public DBCfg getDBcfg(String dbcfg_file) {
		DBCfg cfg = new DBCfg();
		try {
			// propertyReader pro=new Properties();

			FileInputStream fis = new FileInputStream(new File(dbcfg_file));
			Properties properties = new Properties();
			properties.load(fis);
			// pro.
			String url = properties.getProperty("jdbc_url");
			String driver = properties.getProperty("driverClassName");

			String uname = properties.getProperty("jdbc_username");
			String pwd = properties.getProperty("jdbc_password");
			//
			// configuration.configure(propertyFile);
			// String url =
			// configuration.getProperty("connection.url");
			// String driver = configuration
			// .getProperty("connection.driver_class");
			//
			// String uname =
			// configuration.getProperty("connection.username");
			// String pwd =
			// configuration.getProperty("connection.password");
			// // String url =
			// "jdbc:mysql://@host/@db?zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useUnicode=true&characterEncoding=utf8";

			cfg.setUrl(url);
			cfg.setUser(uname);
			cfg.setPassword(pwd);
			cfg.setDriver(driver);
			return cfg;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException("cant ini dbcfg", e);
		}

	}

	public static void main(String[] args) {

		// System.setProperty("dbcfg", "config_oa_formally.properties");
//		System.setProperty("dbcfg", "config.properties");
		new RefreshDeadLimit().start();

	}

	public static Logger logger = LoggerFactory.getLogger(RefreshDeadLimit.class);

	private void start() {
		String dbcfg_file = pathx.webinfDir() + "/" + System.getProperty("dbcfg");
		DbServiceV5r4d db5 = Ioc4wash.getBean(DbServiceV5r4d.class);
		db5.dbcfg = new RefreshDeadLimit().getDBcfg(dbcfg_file); // where
																	// processname
																	// like
																	// '%CS%'
		List<Map> li = db5.executeQuery("select * FROM EZ_FLOW_DE_DESIGNER  where rownum <1000");
		for (Map m : li) {
			try {
				System.out.println(AtiJson.toJson(m));
				String id = (String) m.get("id");
				Clob limit_clob = (Clob) m.get("designerxml");
				Optional<String> limit_str = DbutilX.clob2str(limit_clob);
				if (!limit_str.isPresent())
					continue;
				Optional<Integer> Deadlinelimit = getDeadlinelimitVal(limit_str.get());
				if (  ! Deadlinelimit.isPresent() )
					logger.info(  "proc is not set deadline,proc:"+m.get("PROCESSNAME").toString()+" " );
				if (Deadlinelimit.isPresent()) {
					String sql = "update EZ_FLOW_DE_DESIGNER set \"deadline\"=" + Deadlinelimit.get().toString() +",\"deadline_mill\"=@mil@ where id="+id;
					sql=sql.replace("@mil@", timeUtil.toMillSecFromHour( Deadlinelimit.get() ).toString());
					System.out.println(sql);
					logger.info(sql);
					  db5.executeUpdate(sql);
				}
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}

		}
		System.out.println("--f");
	}

	public static Optional<Integer> getDeadlinelimitVal(String xml) {
		String f = "D:\\0workspace\\atiplat_restapi\\src\\aaaPKg\\flow_design_cs.xml";
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = DocumentHelper.parseText(xml);
		} catch (DocumentException e) {
			ExUtil.throwExV2(e);
		}
		// reader.read(new File(f));
		Element root = document.getRootElement();
		Element process_element = root.element("process");
		Element element_def = root.element("definitions");
		Attribute attribute = process_element.attribute("deadlineLimit");
		if(attribute!=null)
		{
		System.out.println(attribute.getValue());

		return Optional.fromNullable(Integer.parseInt(attribute.getValue()));
		}else
			return Optional.fromNullable(null);
		
	}

}
